'''
Created on Mar 24, 2010

@author: jhlee
'''
from OpenGL.GL import *
from OpenGL.GLU import *
from Vertex3D import *

class Triangle3D:
    def __init__(self, vertices, material, uvs):
        self.v0 = vertices[0]
        self.v1 = vertices[1]
        self.v2 = vertices[2]
        
        self.material = material
        self.uv0 = uvs[0]
        self.uv1 = uvs[1]
        self.uv2 = uvs[2]
        
        self.createNormal()

    def createNormal(self):
        v1 = self.v1.clone()
        v2 = self.v2.clone()
        
        v1.minus(self.v0)
        v2.minus(self.v0)
        
        self.normal = Vertex3D.cross(v1,v2)
        self.normal.normalize()        
        
        
    def drawTriangle(self):
        glBegin(GL_TRIANGLES)
        glTexCoord2f(self.uv0.u, self.uv0.v);   glVertex3f(self.v0.x, self.v0.y, self.v0.z);
        glTexCoord2f(self.uv1.u, self.uv1.v);   glVertex3f(self.v1.x, self.v1.y, self.v1.z);
        glTexCoord2f(self.uv2.u, self.uv2.v);   glVertex3f(self.v2.x, self.v2.y, self.v2.z);
        glEnd()
        
    def render(self):
        self.material.setupForRendering()
        self.drawTriangle()
        
        